package com.SmartCare.utils;

import com.SmartCare.common.dto.medicine.MedicationRecordDTO;
import com.SmartCare.mapper.medicine.MedicationListMapper;
import com.SmartCare.mapper.role.ElderlyInformationMapper;
import com.SmartCare.mapper.role.UserMapper;
import com.SmartCare.pojo.role.ElderlyInformation;
import com.SmartCare.pojo.medicine.MedicationList;
import com.SmartCare.pojo.medicine.MedicationRecord;
import com.SmartCare.pojo.role.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 将药单项转成dto的工具类
 */
@Service
public class MedicationRecordTransToDTO {
    @Resource
    MedicationListTransToDTO medicationListTransToDTO;

    @Resource
    MedicationListMapper medicationListMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    ElderlyInformationMapper elderlyInformationMapper;

    public MedicationRecordDTO transToDTO(MedicationRecord r) {
        MedicationRecordDTO dto = new MedicationRecordDTO();
        dto.setId(r.getId());
        dto.setListNum(r.getListNum());
        dto.setCreateTime(r.getCreateTime());
        dto.setUpdateTime(r.getUpdateTime());
        dto.setStatus(r.getStatus());
        dto.setTime(r.getTime());
        MedicationList medicationList = medicationListMapper.selectById(r.getMedicationListId());
        if (medicationList != null) {
            dto.setMedicationList(medicationListTransToDTO.transToDTO(medicationList));
        }
        ElderlyInformation elder = elderlyInformationMapper.selectById(r.getElderlyId());
        if (elder != null) {
            dto.setElder(elder);
        }
        User nurse = userMapper.selectById(r.getNurseId());
        if (nurse != null) {
            dto.setNurse(nurse);
        }

        return dto;
    }
}
